package com.zzg.springboot.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zzg.springboot.dao.BookRepository;
import com.zzg.springboot.entity.Book;
import com.zzg.springboot.entity.BookDetail;

@Controller
@RequestMapping("/api/springboot")
public class BookController {
	@Autowired
	private BookRepository repository;

	@RequestMapping(value = "/one", method = { RequestMethod.GET })
	public void one() {
		List<Book> books = new ArrayList<>();
		books.add(new Book("Book one", new BookDetail(1)));
		books.add(new Book("Book two", new BookDetail(2)));
		books.add(new Book("Book three", new BookDetail(3)));
		List<Book> bookList = repository.save(books);
	}
	
	@RequestMapping(value = "/two", method = { RequestMethod.GET })
	@ResponseBody
	public List<Book> two() {
		List<Book> books = repository.findAll();
		for (Book book: books) {
            System.out.println(book.toString());
        }
		return books;
	}

}
